//npm install crypto-js --save-dev或cnpm install crypto-js --save-dev

import CryptoJS from 'crypto-js'
// 加密
export default {
  encrypt(word, keyStr, iv) {
    let keys = CryptoJS.enc.Utf8.parse(keyStr); //'82LIb3DQE9ByIdz9'
    let ivs = CryptoJS.enc.Utf8.parse(iv); //iv '0102030405060708'
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, keys, {
      iv: ivs,
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    console.log("base64:" + encrypted);
    var encryptedStr = encrypted.ciphertext.toString();
    console.log("16进制:" + encryptedStr);
    return encrypted.ciphertext.toString();  //16进制
    // return encrypted.toString(); //base64
  },
  // }
  // 解密
  decrypt(word, keyStr, iv) {
    // ********************解密16进制***********************
    let key = CryptoJS.enc.Utf8.parse(keyStr); //'82LIb3DQE9ByIdz9'
    let ivs = CryptoJS.enc.Utf8.parse(iv); //'0102030405060708'
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: ivs,
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    console.log(decryptedStr)
    return decryptedStr.toString();
  }

}
